const app= getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    weekText: {
      type: Array,
      value: ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  },
  lastMonth:'◀',
  nextMonth:  '▶',
  clockData:null,
  clockState:1,
  selected:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that  = this;
    var deviceInfo = wx.getDeviceInfo(); 
    var rentType = options.rentType;
    if(rentType==undefined || rentType==''){
      rentType = 1;
    }  
    let d = app.globalData;
      that.setData({
          width:d.windowWidth,
          height:d.windowHeight,
          statusBarHeight:d.statusBarHeight,
          navHeight:d.navHeight,
          marginTop:d.navTitleTop,
          bottom:d.bottom,
          domain:d.domain,
         imgUrl:d.imgUrl,
          platform:deviceInfo.platform,
          rentType:rentType
        })
  },
  
  scrollCalendar(year, month, date){
    console.log(year, month, date)
    var that = this, scrollLeft = 0;
    wx.getSystemInfo({
        success(res) {
            //切换月份时 date为0
            if (date == 0 ){
                scrollLeft = 0;
                //切换到当年当月 滚动到当日
                if (year == that.data.YEAR && month == that.data.MONTH) {
                    scrollLeft = that.data.DATE * 45 - res.windowWidth / 2 - 22.5;
                }
            }else{
                // 点选具体某一天 滚到到指定日期
                scrollLeft = date * 45 - res.windowWidth / 2 - 22.5;
            }

            that.setData({
                scrollLeft: scrollLeft
            })
        }
    })
},


  display: function (year, month, date) {
    this.setData({
        year,
        month,
        date,
        selected:0,
        title: year + '年' + this.zero(month) + '月'
    })
   // this.getclockDate(year,month,date);
    this.createDays(year, month);
    this.createEmptyGrids(year, month);

    //滚动模糊 初始界面
    this.scrollCalendar(year, month, date);
    //this.getclockDate(year,month,0);
},
  today: function () {
    let DATE = this.data.defaultValue?new Date(this.data.defaultValue) : new Date(),
        year = DATE.getFullYear(),
        month = DATE.getMonth() + 1,
        date = DATE.getDate(),
        select = year + '-' + this.zero(month) + '-' + this.zero(date);
        var timeStamp = new Date(year,month,date,0,0,0);
        var nowTime = timeStamp.getTime();
        console.log('timeStamp='+timeStamp)
        console.log('nowTime='+nowTime)
    this.setData({
        format: select,
        select: select,
        year: year,
        month: month,
        date: date,
        YEAR: year,
        MONTH: month,
        DATE: date,
        nowTime:nowTime
    })

    //初始化日历组件UI
    this.display(year, month, date);

    //发送事件监听
    this.triggerEvent('select', select);
},
select: function (e) {
  console.log(e);
  let date = e.currentTarget.dataset.date;


  var  select = this.data.year + '-' + this.zero(this.data.month) + '-' + this.zero(date);
  this.setData({
      title: this.data.year + '年' + this.zero(this.data.month) + '月' + this.zero(date) + '日',
      select: select,
      year: this.data.year,
      month: this.data.month,
      date:date,
      selected:1
  });
   //发送事件监听
  this.triggerEvent('select', select);

  //滚动日历到选中日期
  this.scrollCalendar(this.data.year, this.data.month,date);
  
},
//上个月
lastMonth: function () {
  let month = this.data.month == 1 ? 12 : this.data.month - 1;
  let year = this.data.month == 1 ? this.data.year - 1 : this.data.year;
  //初始化日历组件UI
  this.display(year, month, 0);
},
//下个月
nextMonth: function () {
  let month = this.data.month == 12 ? 1 : this.data.month + 1;
  let year = this.data.month == 12 ? this.data.year + 1 : this.data.year;
  //初始化日历组件UI
  this.display(year, month, 0);
  
},
//获取当月天数
getThisMonthDays: function (year, month) {
  return new Date(year, month, 0).getDate();
},
// 绘制当月天数占的格子
createDays: function (year, month) {
  let thisMonthDays = [],
      days = this.getThisMonthDays(year, month);
  for (let i = 1; i <= days; i++) {
    var DT = new Date(year,month,i,0,0,0);
    var time = DT.getTime();
      thisMonthDays.push({
          time:time,
          date: i,
          dateFormat: this.zero(i),
          monthFormat: this.zero(month),
          week: this.data.weekText[new Date(Date.UTC(year, month - 1, i)).getDay()]
      });
  }
  this.setData({
      thisMonthDays
  })
},
//获取当月空出的天数
createEmptyGrids: function (year, month) {
  let week = new Date(Date.UTC(year, month - 1, 1)).getDay(),
      empytGridsBefore = [],
      empytGridsAfter = [],
      emptyDays = (week == 0 ? 7 : week);
  //当月天数
  var thisMonthDays = this.getThisMonthDays(year, month);
  //上月天数
  var preMonthDays = month - 1 < 0 ?
       this.getThisMonthDays(year - 1, 12) 
      : this.getThisMonthDays(year, month - 1);

  //空出日期
  for (let i = 1; i <= emptyDays; i++) {
      empytGridsBefore.push(preMonthDays - (emptyDays - i));
  }

  var after = (42 - thisMonthDays - emptyDays) - 7 >= 0 ?
               (42 - thisMonthDays - emptyDays) - 7 
              : (42 - thisMonthDays - emptyDays);
  for (let i = 1; i <= after; i++) {
      empytGridsAfter.push(i);
  }
  this.setData({
      empytGridsAfter,
      empytGridsBefore
  })
},

//补全0
zero: function (i) {
  return i >= 10 ?i : '0' + i;
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.today();
    var rent_type  = wx.getStorageSync('rent_type');
    if(rent_type == undefined || rent_type  == ''){
      wx.request({
        url: app.globalData.domain+'/wechat/home/getRentType',
        data:{from:1},
        success:res=>{
            console.log(res)
            var d=res.data;
        
            console.log(d);
            wx.getStorageSync('rent_type',d.rent_type)
            that.setData({
            rent_type:d.rent_type,
      
        
            })
        }
    })
    }else{
      this.setData({
        rent_type:rent_type
      })
    }
  },
  goBack:function(){
    let d = this.data;
    if(d.selected==1){
      
      var rentType = d.rentType;
      var nextYear ;

   
      var rent_type = d.rent_type;
      rent_type.forEach(function(key,value){
        console.log(key)
        if(rentType==key.id){
          nextYear = d.year + key.year;
        }
      });
      const pages = getCurrentPages();
      const prevPage = pages[pages.length - 2]; // 获取上一个页面实例
      console.log(prevPage)
     var month = this.zero(d.month);
     var day = this.zero(d.date);
      prevPage.setData({
        year: d.year,
        month:month,
        day:day,
        nextYear:nextYear
      });
     wx.navigateBack();
    }else{
      wx.navigateBack();
    }
   // 
  },
  goIndex:function(){
    wx.reLaunch({
      url: '../index/index',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})